package com.cz;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class ConsumerMessage {
    public static void main(String[] args) {
        try {
            // 实例化消费者
            DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ConsumerGroupName",true,null);

            // 设置NameServer地址
            consumer.setNamesrvAddr("172.16.1.210:9876");

            // 订阅Topic
            consumer.subscribe("czbabl_test", "*");
            consumer.setConsumerGroup("czbabl_test");

            // 注册消息监听
            consumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    // 消费消息
                    for (MessageExt msg : msgs) {
                        System.out.println("消费消息内容:"+new String(msg.getBody()));
                    }
                    // 消费成功
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });

            // 启动消费者
            consumer.start();
        } catch (MQClientException e) {
            e.printStackTrace();
        }
    }
}
